package org.oa.ai.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.oa.ai.feign.AIFeignService;
import org.oa.ai.chatModel.OactbdCsqjsq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ToolService {

    @Autowired
    private AIFeignService aiFeignService;


    @Tool(value = "保存请假数据", name = "submitQjsq")
    public Map<String, Object> submitQjsq(
            @P("请假事由") String qjsy,
            @P("申请人") String sqr,
            @P("申请日期") String sqrq,
            @P("申请人部门") String loginDeptName,
            @P("账号id") String userId,
            @P("部门id") String deptId
    ) {
        String genId = IdUtil.simpleUUID();
        OactbdCsqjsq oactbdCsqjsq = new OactbdCsqjsq();
        oactbdCsqjsq.setId(genId);   // 设置id
        oactbdCsqjsq.setTitle(qjsy);  // 设置请假事由
        oactbdCsqjsq.setSqr(sqr);  // 设置请假人
        oactbdCsqjsq.setSqrq(sqrq);  // 设置请假日期
        oactbdCsqjsq.setSqrbm(loginDeptName);  // 设置请假人部门
        oactbdCsqjsq.setCreateusername(sqrq);
        oactbdCsqjsq.setCreateuserid(userId);
        oactbdCsqjsq.setCreatedeptname(loginDeptName);
        oactbdCsqjsq.setCreatedeptid(deptId);
        oactbdCsqjsq.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        oactbdCsqjsq.setMjid("10");
        oactbdCsqjsq.setMjname("公开");
        oactbdCsqjsq.setYwlx("csqjsq");
        oactbdCsqjsq.setYwname("测试请假申请");
        oactbdCsqjsq.setFormtype("6");
        oactbdCsqjsq.setFormversion("17");
        oactbdCsqjsq.setStatus("10");
        oactbdCsqjsq.setOaunitid("Qe16988cc2dd2759");
        oactbdCsqjsq.setOaunitpath("0.orgRootDomain.Qe16982f7b9d2ad4.Qe16988cc2dd2759");
        aiFeignService.add(oactbdCsqjsq);
        return Map.of(
                "result", oactbdCsqjsq,
                "qjId", genId, // 显式返回便于调试
                "metadata", Map.of("instructions", List.of("STORE qjId=" + genId))
        );
    }

    @Tool("查询请假数据")
    public OactbdCsqjsq selectQjById(@P("qjId") String qjId) {
        OactbdCsqjsq map = aiFeignService.getDataById(qjId);
        return map;
    }


    @Tool("获取请假流程节点")
    public JSONArray getFlowNodeList(@P("qjId") String qjId) {
        // 通过请假数据id查询数据
        OactbdCsqjsq oactbdCsqjsq = aiFeignService.getDataById(qjId);
        String ywlx = oactbdCsqjsq.getYwlx();
        JSONArray obj = aiFeignService.nextTasksByNodeId("", ywlx);
        return obj;
    }

    @Tool("获取流程节点人员列表")
    public JSONArray getFlowUserByNodeIdNew (@P("登录用户id") String userId, @P("流程节点id") String nodeId) {
        JSONArray userList = aiFeignService.getFlowUserByNodeId(nodeId, "csqjsq", userId, userId);
        return userList;
    }

    @Tool("提交请假流程")
    public String taskComplete(
            @P("流程节点id") String nodeId,
            @P("流程节点名称") String nodeName,
            @P("提交的用户id") String userIds,
            @P("提交的用户名称") String userNames,
            @P("qjId") String qjId
    ) {
        OactbdCsqjsq oactbdCsqjsq = aiFeignService.getDataById(qjId);
        // 调用流程启动接口
        String businessKey = oactbdCsqjsq.getId();
        String processDefinitionKey = oactbdCsqjsq.getYwlx();
        String title = oactbdCsqjsq.getTitle();
        String moduleId = oactbdCsqjsq.getYwlx();
        String firstNode = nodeId;
        String reportStatus = "ptyg";
        String mijiid = oactbdCsqjsq.getMjid();
        String tableName = "";
        String comminfoId = "";
        String formTyp = oactbdCsqjsq.getFormtype();
        // 流程启动
        Map<String, Object> flowStartMap = aiFeignService.startProcessByKeyNew(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, oactbdCsqjsq.getCreateuserid(), formTyp);
        // 获取任务信息
        Map<String, Object> taskInfo = (Map<String, Object>) flowStartMap.get("taskInfo");
        if (taskInfo != null) {
            String taskId = taskInfo.get("taskId").toString();
            // 修改流程状态
            Boolean b = aiFeignService.updateReportStatus(taskId, "tjz");
            // 如果修改成功，则流程提交
            if (b) {
                String currentTaskId = taskId;
                String nextTaskActivityId = nodeId;
                String nextTaskUserIds = userIds;
                String nextTaskUsernames = userNames;
                String assigneeId = userIds;
                String nodeCode = "";
                String rwlxs = "ptyg";
                String exField5 = "true";
                String processDefinitionId = "";
                String currentNodeId = "";
                String tjlx = "";
                String logtype = "";
                // 提交流程
                Map<String, Object> map = aiFeignService.taskCompleteNew(
                        businessKey,
                        currentTaskId,
                        nextTaskActivityId,
                        nextTaskUserIds,
                        nextTaskUsernames,
                        assigneeId,
                        moduleId,
                        nodeName,
                        reportStatus,
                        nodeCode,
                        rwlxs,
                        exField5,
                        processDefinitionId,
                        currentNodeId,
                        tjlx,
                        logtype,
                        tableName,
                        comminfoId,
                        processDefinitionKey,
                        formTyp,
                        oactbdCsqjsq.getCreateuserid()
                );
            }
        }
        return "流程已经提交";
    }

}
